package pt.utl.ist.ginger3d.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class Template extends Tag implements IBlockSet {
	
	private LinkedHashMap<String, Block> blockMap;
	private List<PlaceHolderBlock> placeHolders;

	public Template(Template baseTemplate, String name, Map<String, String> properties) {
		super(name, properties);
		blockMap = new LinkedHashMap<String, Block>();
		placeHolders = new ArrayList<PlaceHolderBlock>();
		
		if(baseTemplate != null) {
			for(Block block : baseTemplate.getBlocks()) {
				blockMap.put(block.getName(), block.doCopy());
			}
			updateProperties(baseTemplate.getProperties(), false);
		}
	}
	
	public void addBlock(Block block) {
		blockMap.put(block.getName(), block);
		
		if(block instanceof PlaceHolderBlock) {
			placeHolders.add((PlaceHolderBlock)block);
		}
	}

	@Override
	protected String[] getRequiredProperties() {
		return null;
	}

	public Block getBlock(String blockName) {
		return blockMap.get(blockName);
	}

	public void setBlock(Block newBlock) {
		
	}
	
	@Override
	public String toString() {
		return "template (" + getName()  + " / " + getProperties() + ") " + blockMap.toString();
	}

	public Collection<Block> getBlocks() {
		return blockMap.values();
	}

	public boolean containsBlock(String blockName) {
		return getBlock(blockName) != null;
	}
}
